//
//  UITableView+YMPlaceholder.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/10/23.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "UITableView+YMPlaceholder.h"
#import <objc/runtime.h>
#import "PlaceholderView.h"


@implementation UITableView (YMPlaceholder)


+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self methodSwizzlingWithOriginalSelector:@selector(reloadData) bySwizzledSelector:@selector(yy_reloadData)];
    });
}

- (void)yy_reloadData{
    
    id <UITableViewDataSource> dataSource = self.dataSource;
    NSInteger sections = 1;
    BOOL showNoData = NO;
    if ([dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        sections = [dataSource numberOfSectionsInTableView:self] - 1;
        showNoData = sections > 0;
    }
    
    for (NSInteger i = 0; i <= sections; i++) {
        NSInteger rows = [dataSource tableView:self numberOfRowsInSection:i];
//        if (sections == 1) {
            showNoData = rows > 0;
//        }
    }
    
    if (self.placeholderView) {
        [self.placeholderView removeFromSuperview];
        self.placeholderView = nil;
    }

    if (!showNoData) {
        [self addPlaceholder];
    }
    
    [self yy_reloadData];
}

- (void)addPlaceholder{
    if (!self.placeholderView) {
        PlaceholderView *placeholderView = [[PlaceholderView alloc]initWithFrame:self.bounds orginy:64 reloadBtn:self.reloadBtn];
        placeholderView.titleLabel.text = self.tipsStr ? self.tipsStr : @"no data";
        placeholderView.imageView.image = self.tipsImg ? self.tipsImg : [UIImage imageNamed:@"icon_message_record"];
        WEAKSELF
        placeholderView.reloadClickBlock = ^{
            if (weakSelf.reloadClickBlock) {
                weakSelf.reloadClickBlock();
            }
        };
        self.placeholderView = placeholderView;
    }
    
    [self addSubview:self.placeholderView];
}

- (void)setPlaceholderView:(PlaceholderView *)placeholderView{
    objc_setAssociatedObject(self, @selector(placeholderView), placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (PlaceholderView *)placeholderView{
    return objc_getAssociatedObject(self, @selector(placeholderView));
}

- (void)setTipsImg:(UIImage *)tipsImg{
    objc_setAssociatedObject(self, @selector(tipsImg), tipsImg, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImage *)tipsImg{
    return objc_getAssociatedObject(self, @selector(tipsImg));
}

- (void)setTipsStr:(NSString *)tipsStr{
    objc_setAssociatedObject(self, @selector(tipsStr), tipsStr, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)tipsStr{
    return objc_getAssociatedObject(self, @selector(tipsStr));
}

- (void)setReloadBtn:(BOOL)reloadBtn{
    objc_setAssociatedObject(self, @selector(reloadBtn), @(reloadBtn), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)reloadBtn{
    return [objc_getAssociatedObject(self, @selector(reloadBtn)) boolValue];
}

- (void)setReloadClickBlock:(void (^)(void))reloadClickBlock{
    objc_setAssociatedObject(self, @selector(reloadClickBlock), reloadClickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(void))reloadClickBlock{
    return objc_getAssociatedObject(self, @selector(reloadClickBlock));
}

+ (void)methodSwizzlingWithOriginalSelector:(SEL)originalSelector bySwizzledSelector:(SEL)swizzledSelector{
    Class class = [self class];
    
    // 原有方法
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    
    // 替换原有方法的新方法
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    //先尝试給源SEL添加IMP，这里是为了避免源SEL没有实现IMP的情况
    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {//添加成功：说明源SEL没有实现IMP，将源SEL的IMP替换到交换SEL的IMP
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }else{//添加失败：说明源SEL已经有IMP，直接将两个SEL的IMP交换即可
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}
@end
